//
//  MineViewController.m
//  ShanghaiShopping
//
//  Created by KDC on 2019/8/22.
//  Copyright © 2019 KDC. All rights reserved.
//

#import "MineViewController.h"
#import "CollectViewController.h"
#import "AdviceViewController.h"
#import "SetViewController.h"
#import "RegisterSetUserInfoVC.h"

@interface MineViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *tableHeaderView;
@property (nonatomic, strong) NSDictionary *dicData;

@property (nonatomic, strong) UIButton *userImageBtn;
@property (nonatomic, strong) UIButton *userNameBtn;
@property (nonatomic, strong) UILabel *userMobilLb;
@property (nonatomic, assign) BOOL isLogin;

@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    
    self.isLogin = [[UserManager sharedUserManager] isLogin];
    
    if (self.isLogin) {
        NSString *mobile = [UserManager sharedUserManager].userInfo.mobile;
        NSString *string = [mobile stringByReplacingOccurrencesOfString:[mobile substringWithRange:NSMakeRange(3, 4)] withString:@"****"];
        self.userMobilLb.text = string;
        
        [self.userNameBtn setTitle:[UserManager sharedUserManager].userInfo.nickName forState:UIControlStateNormal];
        [self.userImageBtn sd_setImageWithURL:[NSURL URLWithString:@""] forState:UIControlStateNormal placeholderImage:ImageNamed(@"启动页png")];
    } else {
        self.userMobilLb.text = @"侬好";
        [self.userNameBtn setTitle:@"早安上海" forState:UIControlStateNormal];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dicData = @{@"title":@[
                                @"联系客服",
                                @" 意见反馈",
                                @"设置"],
                     @"image":@[
                                @"收藏_line",
                                @"订单_line",
                                @"设置_line"]
                     };
    [self addNav];
    [self.view addSubview:self.tableView];
}

- (void)addNav {
    CGFloat navHeight = kNavBarAndStatusBarHeight + 30;
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, navHeight)];
    navView.backgroundColor = Color_APP;
    [self.view addSubview:navView];
    
    UILabel *lbTitle = [[UILabel alloc] initWithFrame:CGRectMake(16, navHeight - 34 - 11, 50, 34)];
    lbTitle.text = @"我的";
    lbTitle.textColor = UIColor.whiteColor;
    lbTitle.font = FONT_BOLD(24);
    [navView addSubview:lbTitle];
}





#pragma mark --UITableView代理

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dicData[@"title"] count];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    switch (indexPath.row) {
        case 0:{
//            if (self.isLogin) {
//                CollectViewController *vc = [[CollectViewController alloc] init];
//                vc.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:vc animated:YES];
//            } else {
//                LoginAndRegisterVC *vc = [[LoginAndRegisterVC alloc] init];
//                vc.pushType = 1;
//                vc.hidesBottomBarWhenPushed = YES;
//                [self.navigationController pushViewController:vc animated:YES];
//            }
            
            [self contactQQ];
        }
            break;
        case 1:{
            AdviceViewController *vc = [[AdviceViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:{
            SetViewController *vc = [[SetViewController alloc] init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identifier = @"MineViewController";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.dicData[@"title"][indexPath.row];
    cell.textLabel.font = FONT(14);
    cell.imageView.image = ImageNamed(self.dicData[@"image"][indexPath.row]);
    
    UIImageView *accessoryImgView = [[UIImageView alloc] initWithImage:ImageNamed(@"箭头")];
    cell.accessoryView = accessoryImgView;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight + 30, SCREEN_WIDTH, SCREEN_HEIGHT - (kNavBarAndStatusBarHeight + 30) - kTabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.tableHeaderView;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (UIView *)tableHeaderView {
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 152 / 375)];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionLogin)];
        [_tableHeaderView addGestureRecognizer:tap];
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:_tableHeaderView.frame];
        imageView.image = ImageNamed(@"路径 3");
        [_tableHeaderView addSubview:imageView];
        
        UIButton *btnIcon = [[UIButton alloc] initWithFrame:CGRectMake(42, 24, 60, 60)];
        btnIcon.layer.cornerRadius = 30;
        btnIcon.layer.masksToBounds = YES;
        self.userImageBtn = btnIcon;
        [btnIcon setImage:ImageNamed(@"启动页png") forState:UIControlStateNormal];
        [_tableHeaderView addSubview:btnIcon];
        
        UIButton *btnName = [[UIButton alloc] initWithFrame:CGRectMake(113, 30, SCREEN_WIDTH - 113, 25)];
        [_tableHeaderView addSubview:btnName];
        btnName.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [btnName addTarget:self action:@selector(actionLogin) forControlEvents:UIControlEventTouchUpInside];
        [btnName setTitle:@"早安上海" forState:UIControlStateNormal];
        btnName.titleLabel.font = FONT_BOLD(18);
        self.userNameBtn = btnName;
        
        UILabel *lbPhoneNum = [[UILabel alloc] initWithFrame:CGRectMake(113, 59, SCREEN_WIDTH - 113, 19)];
        [_tableHeaderView addSubview:lbPhoneNum];
        lbPhoneNum.text = @"侬好";
        lbPhoneNum.textColor = UIColorFromRGB(0xFFFFFF);
        lbPhoneNum.font = FONT(13);
        self.userMobilLb = lbPhoneNum;
    }
    return _tableHeaderView;
}

- (void)actionLogin {
    return;
    
    if (self.isLogin) {
        RegisterSetUserInfoVC *vc = [[RegisterSetUserInfoVC alloc] init];
        vc.isLogin = YES;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        LoginAndRegisterVC *vc = [[LoginAndRegisterVC alloc] init];
        vc.pushType = 1;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)contactQQ {
    // 判断手机是否安装QQ
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
        NSString *QQ = @"1014254142";
        NSString *qq = [NSString stringWithFormat:@"mqq://im/chat?chat_type=wpa&uin=%@&version=1&src_type=web", QQ];
        NSURL *url = [NSURL URLWithString:qq];
        [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
        
    } else {
        [SXLoadingView showAlertHUD:@"请先下载安装QQ哦" duration:1];
    }
}


@end
